 package edu.vtc.clacky;

import java.util.ArrayList;


public class InMemoryStorage implements Storage {
	
	private ArrayList<Rational> values;
	private int registerCountInt;
	
	public InMemoryStorage()
	{
		values = new ArrayList<Rational>();
	}
	
    @Override
    public void store(int registerNumber, Rational value) throws BadRegisterException
    {
    	if (registerNumber < 0) {
    		String message = String.format("The register number %d is invalid.\n", registerNumber);
    		throw new BadRegisterException(message);
    	}
    	else if (values.size() <= registerNumber){
    		for (int i = 0; i <= registerNumber; i++) {
    			values.add(new Rational(0, 1));
    		}
    	}
    	values.set(registerNumber, value);
    	registerCountInt++;
    	
    }

    @Override
    public Rational recall(int registerNumber) throws BadRegisterException {
    	if (registerNumber < 0) {
    		String message = String.format("The register number %d is invalid.\n", registerNumber);
    		throw new BadRegisterException(message);
    	}
    	else {
        Rational value = values.get(registerNumber);
        values.set(registerNumber, new Rational(0, 1));
        registerCountInt--;
        return value;
    	}
    }
    

	@Override
	public Rational registerCount() {
		String registerCountString = Integer.toString(registerCountInt);
		Rational registerCount = new Rational(registerCountString);
		return registerCount;
	}
	
}


